const co = require('co')
const fetch = require('node-fetch')

co(function *(){
    const res = yield fetch('https://api.douban.com/v2/movie/1291843')
    const movie = yield res.json()
    const summary = movie.summary
    console.log(summary)
})

function run (generator) {
    // 返回一个generator对象
    const iterator = generator()
    // 调用next(),返回一个对象，包含第一个yield返回的promise对象的value
    const it = iterator.next()
    // 返回value中的promise对象
    const promise = it.value
    
    promise.then(data => {
        // 再次调用next()函数，返回第二个yield返回的promise对象
        const it2 = iterator.next(data)
        const promise2 = it2.value
        promise2.then(data2 => {
            iterator.next(data2)
        })

    })
}

// run(function *(){
//     const res = yield fetch('https://api.douban.com/v2/movie/1291843')
//     const movie = yield res.json()
//     const summary = movie.summary
//     console.log(summary)
// })